"ui";
// require("./first启动欢迎页")
let workThread=null; 
var color = "#009688";

ui.layout(
    <drawer id="drawer">
        <vertical>
        <TextView
        id="tv_text"
        singleLine="true"
        ellipsize="marquee"
        focusable="true"
        text="如出现错误请联系QQ1184664659修复  如有任何出现错误请联系QQ1184664659修复 如有任何出现错误请联系QQ1184664659修复"
        textColor="#ff010100"/>
            <appbar>
                <toolbar id="toolbar" title="wenke_tools"/>
                <tabs id="tabs"/>
            </appbar>
            <viewpager id="viewpager">
                <vertical>
                    //可开关按钮
                    <Switch id="autoService" w="*" checked="{{auto.service != null}}" textColor="#666666" text="无障碍服务"/>           
                    <button textSize="24sp" w="auto" h="auto" text="疫情常态化" id="cth" />
                    <button textSize="24sp" w="auto" h="auto" text="掘金签到" id="jj" />
                    <button textSize="24sp" w="auto" h="auto" text="飞桨签到" id="fj" />
                    <button textSize="24sp" w="auto" h="auto" text="一言" id="yiy" />
                    <button id="start" text="开始运行" tag="ScriptTag" color="#ffffff" bg="#FF4FB3FF" foreground="?selectableItemBackground"/>
                </vertical>
                <frame>
                    <text text="第二页内容" textColor="red" textSize="16sp"/>
                </frame>
                <frame>
                    <text text="第三页内容" textColor="green" textSize="16sp"/>
                </frame>
            </viewpager>
        </vertical>
        <vertical layout_gravity="left" bg="#ffffff" w="280">
            <img w="280" h="200" scaleType="fitXY" src="http://images.shejidaren.com/wp-content/uploads/2014/10/023746fki.jpg"/>
            <list id="menu">
                <horizontal bg="?selectableItemBackground" w="*">
                    <img w="50" h="50" padding="16" src="{{this.icon}}" tint="{{color}}"/>
                    <text textColor="black" textSize="15sp" text="{{this.title}}" layout_gravity="center"/>
                </horizontal>
            </list>
        </vertical>
        
    </drawer>
)
ui.tv_text.setSelected(true);//跑马灯
//回到本界面时，resume事件会被触发
ui.emitter.on("resume",()=>{
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});
//禁止返回退出脚本
ui.emitter.on("back_pressed",function(event){
    if(workThread&&workThread.isAlive()){
        backTag=true;
        toast("为防止脚本自动退出，脚本运行时不可返回退出软件");
        event.consumed=true;
    }
})
//无障碍开关监控
ui.autoService.setOnCheckedChangeListener(function(widget,checked) {
    if(checked&&!auto.service) {     
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if(!checked&&auto.service)auto.service.disableSelf()
    ui.autoService.setChecked(auto.service!=null) 
});

//id为cth的按钮点击事件
ui.cth.on("click",()=>{
threads.start(function() {
require("./常态化");
});
});

ui.jj.on("click",()=>{
threads.start(function() {
require("./掘金签到");
});
});

ui.fj.on("click",()=>{
threads.start(function() {
require("./百度飞桨签到");
});
});

ui.yiy.on("click",()=>{
require("./一言");

});


ui.start.click(()=>{
    ui.start.setText("停止运行");        
    workThread=threads.start(function(){
       try{
          clearLog()
          if(!auto.service)toast("请先打开无障碍服务");         
          else workMain()
       }catch(e){
          if(!e.javaException instanceof java.lang.InterruptedException)  
             console.error("运行出错:"+e.toString())
       }finally{
          ui.run(function(){
             ui.start.setText("开始运行")        
          });
       }
    });
 });

//创建选项菜单(右上角)
ui.emitter.on("create_options_menu", menu => {    
    menu.add("日志");
    menu.add("问题反馈");
    menu.add("QQ群交流群");
    menu.add("关于");
});
//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item) => {
    switch (item.getTitle()) {
        case "日志":
            app.startActivity('console');
            break;
        case "QQ群交流群":
        app.startActivity({
            action: "android.intent.action.VIEW",
            data:"mqqapi://card/show_pslcard?card_type=group&uin=260993045",
            packageName: "com.tencent.mobileqq",
        });
            break;    
        case "问题反馈":
            反馈.activate();
            break;
        case "关于":
            首页.activate();
            break;        
    }
    e.consumed = true;
});






activity.setSupportActionBar(ui.toolbar);

//设置滑动页面的标题
ui.viewpager.setTitles(["日常任务", "标签二", "标签三"]);
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);

//让工具栏左上角可以打开侧拉菜单
ui.toolbar.setupWithDrawer(ui.drawer);

ui.menu.setDataSource([{
        title: "选项一",
        icon: "@drawable/ic_android_black_48dp"
    },
    {
        title: "选项二",
        icon: "@drawable/ic_settings_black_48dp"
    },
    {
        title: "选项三",
        icon: "@drawable/ic_favorite_black_48dp"
    },
    {
        title: "退出",
        icon: "@drawable/ic_exit_to_app_black_48dp"
    }
]);

ui.menu.on("item_click", item => {
    switch (item.title) {
        case "退出":
            ui.finish();
            break;
    }
})